package com.tianzehua.netty.httpexample;

import io.netty.bootstrap.ServerBootstrap;
import io.netty.channel.ChannelFuture;
import io.netty.channel.EventLoopGroup;
import io.netty.channel.nio.NioEventLoopGroup;
import io.netty.channel.socket.nio.NioServerSocketChannel;

/**
 * TestServer
 *
 * @author tianzehua
 * @date 2019/03/07
 */
public class TestServer {

    public static void main(String[] args) throws Exception {
        /* NioEventLoopGroup 就是一个死循环的线程连接(事件循环组) */
        /* bossGroup就是不断地从客户端接收数据，但是不进行处理，真正处理的是workerGroup这个线程组 */
        EventLoopGroup bossGroup = new NioEventLoopGroup();
        EventLoopGroup workerGroup = new NioEventLoopGroup();
        try {
            /* ServerBootstrap netty服务的引导类 */
            ServerBootstrap serverBootstrap = new ServerBootstrap();
            /* childHandler是自定义的消息处理器，没有的话定义为null */
            serverBootstrap.group(bossGroup,workerGroup)
                    .channel(NioServerSocketChannel.class)
                    .childHandler(new TestServerInitializer());
            /* 定义相应的端口 */
            ChannelFuture sync = serverBootstrap.bind(8899).sync();
            sync.channel().closeFuture().sync();
        } finally {
            /* 关闭线程连接 */
            bossGroup.shutdownGracefully();
            workerGroup.shutdownGracefully();
        }



    }
}
